package by.grsu.samusev.page.pages;

import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.samusev.app.SamusevWebApplication;
import by.grsu.samusev.dao.impl.RequestDao;
import by.grsu.samusev.entity.Request;

public class RequestListPage extends WebPage {
	private final RequestDao requestDao;

	private static final SimpleDateFormat sdf = new SimpleDateFormat("DD.mm.yyyy HH:MM");

	public RequestListPage() {
		super();
		requestDao = new RequestDao(SamusevWebApplication.DATA_XML_FOLDER);
		someshit(requestDao.getAll());
	}

	public RequestListPage(List<Request> requests) {
		super();
		requestDao = new RequestDao(SamusevWebApplication.DATA_XML_FOLDER);
		someshit(requests);
	}

	private void someshit(final List<Request> requests) {
		add(new DataView<Request>("table-rows", new ListDataProvider<Request>(requests)) {
			@Override
			protected void populateItem(final Item<Request> item) {
				final Request request = item.getModelObject();

				item.add(new Label("id", request.getId()));
				item.add(new Label("VoyageId", request.getVoyageId()));
				item.add(new Label("date", sdf.format(request.getDate())));



				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						setResponsePage(new RequestEditPage(request));
					}
				});

				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						requestDao.delete(request.getId());
						setResponsePage(new RequestListPage());
					}
				});
			}
		});
	}
}